package com.woniu.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.dao.RbacManagerDao;
import com.woniu.entity.RbacManager;
import com.woniu.entity.RbacManagerDTO;
import com.woniu.entity.ResponseEntity;
import com.woniu.utils.SecurityCryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: WenXin
 * @Todo: Manger业务层实现类
 * @DateTime: 22/08/22/0022 10:56
 * @Component: 成都蜗牛学苑
 **/
//@Service+@Autowired
//等同于在xml中的 <bean id =“value的值” class =“RbacManagerServiceImpl类的全限定类名” autowire="byType"/>
@Service
//@Scope(value = "prototype")
public class RbacManagerServiceImpl implements RbacManagerService {
    @Autowired
    private RbacManagerDao rbacManagerDao;

    @Override
    public RbacManager findOne(int id) {
        System.out.println("这个是一个日志：我记录了id是：" + id + "的用户的查询操作！");
        RbacManager one = rbacManagerDao.findOne(id);
        if (Objects.nonNull(one)) {
            System.out.println("查询" + id + "用户成功！");
        } else {
            System.out.println("查询" + id + "用户失败！");
        }
        return one;
    }

    @Override
    public ResponseEntity findAll(int pageNum, int pageSize) {
        //配置开始分页参数
        PageHelper.startPage(pageNum, pageSize);
        List<RbacManager> all = rbacManagerDao.findAll();
        //封装分页结果
        PageInfo<RbacManager> rbacManagerPageInfo = new PageInfo<>(all);
        return new ResponseEntity(200, "查询成功", rbacManagerPageInfo);
    }

    @Override
    @Transactional
    public int addOne(RbacManager rbacManager) {
        String s = SecurityCryUtils.automaticallyGeneratePassword(rbacManager.getAccount(), rbacManager.getPassword());
        rbacManager.setPassword(s);
        int i = rbacManagerDao.addOne(rbacManager);
        return i;
    }

    @Override
    public int updateOne(RbacManager rbacManager) {
        return rbacManagerDao.updateOne(rbacManager);
    }

    @Override
    public int deleteOne(int id) {
        return rbacManagerDao.deleteOne(id);
    }

    @Override
    public ResponseEntity findByrbacManagerDTO(RbacManagerDTO rbacManagerDTO, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RbacManager> findByrbacManagerDTO = rbacManagerDao.findByrbacManagerDTO(rbacManagerDTO);
        PageInfo<RbacManager> rbacManagerPageInfo = new PageInfo<>(findByrbacManagerDTO);
        return new ResponseEntity(200, "搜索结果如下", rbacManagerPageInfo);
    }

    @Override
    public List<RbacManager> findByIds(Integer[] ids) {
        return rbacManagerDao.findByIds(ids);
    }

    @Override
    public int addOneForMap(Map<String, Object> userinfo) {
        return rbacManagerDao.addOneForMap(userinfo);
    }

    @Override
    public int findCount(RbacManager rbacManager) {
        int count = rbacManagerDao.findCount(rbacManager);
        return count;
    }

    @Override
    public RbacManager findOneByName(String username) {

        return rbacManagerDao.findOneByName(username);
    }

    @Override
    public ResponseEntity editStatus(int id) {
        int i = rbacManagerDao.editStatus(id);
        if (i > 0) {
            return new ResponseEntity(200, "更新" + id + "的用户状态成功！", null);
        } else {
            return new ResponseEntity(500, "未知错误更新失败", null);
        }

    }

    //重置密码的方法
    @Override
    public ResponseEntity initPwd(int id) {
        RbacManager rbacManager = rbacManagerDao.findOne(id);
        //初始化用户密码 123456  Hash算法都是非对称加密(不能解密)  提到外面去！
        String newPassword = SecurityCryUtils.automaticallyGeneratePassword(rbacManager.getAccount(), "123456");

        int i = rbacManagerDao.updateInitPwd(id, newPassword);
        if (i > 0) {
            return new ResponseEntity(200, "成功更新" + id + "这条密码成功", null);
        } else {
            return new ResponseEntity(500, "更新失败", null);
        }
    }

    //更新角色权限
    @Override
    public ResponseEntity updateRole(RbacManagerDTO rbacManagerDTO) {
        //判断是否存在同名账户
        int count = rbacManagerDao.findCount(rbacManagerDTO);
        if (count > 1) {
            return new ResponseEntity(500, "修改失败,账号同名！", null);
        }
        String s = SecurityCryUtils.automaticallyGeneratePassword(rbacManagerDTO.getAccount(), rbacManagerDTO.getPassword());
        rbacManagerDTO.setPassword(s);
        int i = rbacManagerDao.updateRole(rbacManagerDTO);
        if (i > 0) {
            return new ResponseEntity(200, "成功更新’" + rbacManagerDTO.getAccount() + "‘用户的角色", null);
        } else {
            return new ResponseEntity(500, "更新失败", null);
        }
    }


}
